package main

import (
	"fmt"
	"strings"

	"gitee.com/lflxp/tuiGo/pkg"
	"github.com/nsf/termbox-go"
)

var current string
var curev termbox.Event

func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		x++
	}
}

func mod_str(m termbox.Modifier) string {
	var out []string
	if m&termbox.ModAlt != 0 {
		out = append(out, "ModAlt")
	}
	if m&termbox.ModMotion != 0 {
		out = append(out, "ModMotion")
	}
	return strings.Join(out, " | ")
}

func draw(xxx, leng int, ss bool, info string) {
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
	x, y := termbox.Size()

	// tbprint(1, 1, termbox.ColorYellow, termbox.ColorBlue, fmt.Sprintf("Height %d Width %d", y, x))
	tbprint(1, 1, termbox.ColorYellow, termbox.ColorBlue, fmt.Sprintf("%d Height %d Width %d %d %c %s %t", xxx, y, x, curev.Key, curev.Ch, mod_str(curev.Mod), ss))
	tbprint(1, 2, termbox.ColorYellow, termbox.ColorBlue, fmt.Sprintf("%d %s", leng, info))
	for i := 0; i <= y; i++ {
		// tbprint(0,mn,termbox.ColorWhite,termbox.ColorYellow,"|")
		termbox.SetCell(0, i, '│', termbox.ColorWhite, termbox.ColorDefault)
		termbox.SetCell(x-1, i, '│', termbox.ColorWhite, termbox.ColorDefault)
	}
	for ii := 0; ii <= x; ii++ {
		termbox.SetCell(ii, 0, '─', termbox.ColorWhite, termbox.ColorDefault)
		termbox.SetCell(ii, y-1, '─', termbox.ColorWhite, termbox.ColorDefault)
	}

	tbprint(2, y-2, termbox.ColorGreen, termbox.ColorDefault, fmt.Sprintf("Press Esc to quit"))
	termbox.Flush()
}

func draw1() {
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
	x, y := termbox.Size()

	tbprint(1, 1, termbox.ColorYellow, termbox.ColorBlue, "This is demo 2")
	for i := 0; i <= y; i++ {
		// tbprint(0,mn,termbox.ColorWhite,termbox.ColorYellow,"|")
		termbox.SetCell(0, i, '│', termbox.ColorWhite, termbox.ColorDefault)
		termbox.SetCell(x-1, i, '│', termbox.ColorWhite, termbox.ColorDefault)
	}
	for ii := 0; ii <= x; ii++ {
		termbox.SetCell(ii, 0, '─', termbox.ColorWhite, termbox.ColorDefault)
		termbox.SetCell(ii, y-1, '─', termbox.ColorWhite, termbox.ColorDefault)
	}
	tbprint(2, y-2, termbox.ColorGreen, termbox.ColorDefault, fmt.Sprintf("Press q to quit"))
	termbox.Flush()
}

func main() {
	x := 0
	err := termbox.Init()
	if err != nil {
		panic(err)
	}

	defer termbox.Close()

	termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
	termbox.Clear(termbox.ColorBlack, termbox.ColorDefault)

	ctrlxpressed := false

	data := make([]byte, 0, 64)
	draw(x, len(data), ctrlxpressed, string(data))
	sx := 2
	sy := 4
	lx := 10
	ly := 10

mainloop:
	for {
		x++
		if cap(data)-len(data) < 32 {
			newdata := make([]byte, len(data), len(data)+32)
			copy(newdata, data)
			data = newdata
		}
		beg := len(data)
		d := data[beg : beg+32]
		switch ev := termbox.PollRawEvent(d); ev.Type {
		case termbox.EventRaw:
			data = data[:beg+ev.N]
			current = fmt.Sprintf("%q", data)
			if current == `"q"` {
				break mainloop
			}

			for {
				ev := termbox.ParseEvent(data)
				if ev.N == 0 {
					break
				}
				curev = ev
				copy(data, data[curev.N:])
				data = data[:len(data)-curev.N]

				if curev.Key == termbox.KeyCtrlE && ctrlxpressed {
					draw1()
					break
				} else if curev.Key == termbox.KeyCtrlX {
					ctrlxpressed = true
					draw(x, beg, ctrlxpressed, string(data))
				} else if curev.Key == termbox.KeyF1 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					tbprint(0, 0, termbox.ColorYellow, termbox.ColorDefault, "HELP PAGES")
					termbox.Flush()
				} else if curev.Key == termbox.KeyArrowUp {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					sy--
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					// con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyArrowDown {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					sy++
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyArrowLeft {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					sx--
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					// con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyArrowRight {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					sx++
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyCtrl4 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					lx++
					// ly++
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyCtrl3 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					lx--
					// ly--
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyCtrl7 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					// lx++
					ly++
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyCtrl6 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					// lx--
					ly--
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyF2 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					// 新建一个container
					con := pkg.Container{}
					con.StartX = sx
					con.StartY = sy
					con.LengthX = lx
					con.LengthY = ly
					con.Fg = termbox.ColorGreen
					con.Bg = termbox.ColorDefault
					con.Border()
					// 添加一个row
					row := pkg.Row{}
					row.StartX = sx + 1
					row.StartY = sy + 1
					row.LengthX = lx - 2
					row.LengthY = ly - 2
					row.Fg = termbox.ColorRed
					row.Bg = termbox.ColorDefault
					row.AutoRender()
					termbox.Flush()
				} else if curev.Key == termbox.KeyF3 {
					termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
					// 新建一个container
					con := pkg.Container{}
					con.Init().Border()
					termbox.Flush()
				} else {
					ctrlxpressed = false
					draw(x, beg, ctrlxpressed, string(data))
				}
			}
		case termbox.EventResize:
			// draw(x, ctrlxpressed)
			draw1()
		case termbox.EventError:
			panic(ev.Err)
		}
	}
}
